package com.enthal.basicpers

import android.Manifest
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.telephony.TelephonyManager
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat.startActivity
import permissions.dispatcher.*

class RuntimePers (private val contxt: Context) {

    @NeedsPermission(Manifest.permission.READ_PHONE_STATE)
    fun getPhoneIMEI(): String {
        try {
            val tm =
                contxt.getSystemService(Service.TELEPHONY_SERVICE) as TelephonyManager
            return tm.getDeviceId()
        } catch (e: Exception) {}
        return ""
    }

    @OnPermissionDenied(Manifest.permission.READ_PHONE_STATE)
    fun onDenied() {
        Toast.makeText(contxt, "per denied", Toast.LENGTH_SHORT).show()
    }

    @OnNeverAskAgain(Manifest.permission.READ_PHONE_STATE)
    fun onNeverAskAgain() {
        Toast.makeText(contxt, "per no asked",  Toast.LENGTH_SHORT).show()
    }

    @OnShowRationale(Manifest.permission.READ_PHONE_STATE)
    fun showRationaleForCamera(request: PermissionRequest) {
        showRationaleDialog("we need", request)
    }

    private fun showRationaleDialog( messageResId: String, request: PermissionRequest) {
        AlertDialog.Builder(contxt)
            .setPositiveButton("允许") { _, _ -> request.proceed() }
            .setNegativeButton("拒绝") { _, _ -> request.cancel() }
            .setCancelable(false)
            .setMessage(messageResId)
            .show()
    }
}